<template>
  <div>
    <el-form ref="form" :model="formData" inline size="mini">
      <el-form-item>
        <el-input
          v-model="formData.materialName"
          placeholder="请输入物料名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="formData.batchNumber"
          placeholder="请输入物料批次"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="formData.materialType"
          placeholder="请选择物料类型"
          clearable
        >
          <el-option
            :label="item.label"
            :value="item.label"
            v-for="(item, index) in materialType"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column
        :prop="item.prop"
        :label="item.label"
        align="center"
        v-for="(item, index) in materialOverdueTableHeard"
        :key="index"
      >
      </el-table-column>
      <el-table-column label="操作" prop="prop" width="120" align="center">
        <template slot-scope="{ row }">
          <el-button type="text" size="small" @click="deleteMaterial(row.id)"
            >报废</el-button
          >
        </template>
      </el-table-column>
      <!-- 无数据 -->
      <template slot="empty">
        <div class="noData" v-show="tableData.length === 0">
          <!--  v-show="data.length===0" -->
          <img src="@/assets/images/noData.png" />
          <p>暂无数据</p>
        </div>
      </template>
    </el-table>
    <Pagination :total="total" @pagination="handlePage" :page.sync="formData.pageNum"></Pagination>
  </div>
</template>

<script>
import { materialType, materialOverdueTableHeard } from "./constant/index.js";
import {
  expiration,
  materialScrap,
} from "@/api/material-management/material-overdue";
export default {
  data() {
    return {
      total: 0,
      formData: {
        materialName: "",
        batchNumber: "",
        materialType: "",
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],
      materialType,
      materialOverdueTableHeard,
    };
  },
  created() {
    this.getTableData();
  },
  methods: {
    async getTableData() {
      let res = await expiration(this.formData);
      if (res.code == 200) {
        this.tableData = res.data.list;
        this.total = res.data.total;
      }
    },
    handlePage(val) {
      this.formData.pageNum = val.page;
      this.formData.pageSize = val.limit;
      this.getTableData();
    },
    deleteMaterial(id) {
      this.$alert("确定要报废？", "报废", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        showCancelButton: true,
      })
        .then(async () => {
          let res = await materialScrap(id);
          if (res.code == 200) {
            this.tableData.length > 1 ? this.formData.pageNum : this.formData.pageNum--;
            this.getTableData();
          }
        })
        .catch(() => {});
    },
    search() {
      this.formData.pageNum = 1
      this.getTableData()
    }
  },
};
</script>

<style></style>
